/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/13 19:10
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

;(function () {


    "use strict";

    var api = {
        getGroupList: '/stream/group/getGroupLists',
        saveDeviceUrl: '/stream/device/saveDevice'
    };

    sm.controller('admin-stream-detail-controller', ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {

        // $scope.detail = detailInfo || {};
        $scope.groupLists = [];
        /*
        $scope.detail = {
            deviceId: detailInfo.id,
            deviceName: detailInfo.deviceName,
            groupId: detailInfo.groupId,
            videoUrlHD: detailInfo.videoUrlHD,
            videoUrlSD: detailInfo.videoUrlSD
        };
        */
        $scope.detail = detailInfo;

        (function () {
            $http.get(api.getGroupList).success(function (res) {
                if (res.state) {
                    $scope.groupLists = res.data;
                    angular.forEach($scope.groupLists, function (v) {
                        if (v['id'] == detailInfo['groupId']) {
                            $scope.selectedGroupName = v['groupName'];
                        }
                    });
                }
            });
        })();

        /**
         * 选择分组
         * @param $index
         */
        $scope.selectGroup = function ($index) {
            $scope.detail.groupId = $scope.groupLists[$index]['id'];
            $scope.selectedGroupName = $scope.groupLists[$index]['groupName'];
        };

        $scope.save = function () {

            var det = $scope.detail;
            if (!det.id || !det.deviceName || !det.groupId) {
                Modal.alert('有必填项没有填写');
                return false;
            }

            $http.post(api.saveDeviceUrl, {
                deviceId: $scope.detail.id,
                deviceName: $scope.detail.deviceName,
                groupId: $scope.detail.groupId,
                videoUrlHD: $scope.detail.videoUrlHD,
                videoUrlSD: $scope.detail.videoUrlSD
            }).success(function (res) {
                if (res.state) {
                    Modal.success('修改成功');
                } else {
                    Modal.error('修改失败');
                }
            });

        };

        $scope.viewVideo = function (url) {
            $('#videoModal').modal();
            var flashvars;
            flashvars={
                f: '/assets/library/ckplayer/m3u8.swf',
                a: url, //此处填写购买获取到的视频播放地址
                c: 0,
                p: 1,
                s: 4,
                lv: 1
            };
            var params = {bgcolor: '#FFF', allowFullScreen: true, allowScriptAccess: 'always', wmode: 'transparent'};
            CKobject.embedSWF("/assets/library/ckplayer/ckplayer.swf", "videoPlay", "video", "100%", "100%", flashvars, params);
        };

    }]);

})();